package bookshopSM;
//StudentUI.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.*;
public class BookshopGUI extends JPanel {
    protected JLabel labels[];
    public JTextField fields[];
    protected JButton doTask1, doTask2;
    protected JPanel innerPanel[];
    protected int size;
    public BookshopGUI( String arrayString[] )
    {
        size = arrayString.length;
        labels = new JLabel[ size ];
        fields = new JTextField[ size ];
        innerPanel=new JPanel[size+1];

        for ( int count = 0; count < labels.length; count++ ){
            labels[ count ] = new JLabel(arrayString[ count ]);
        }
        for ( int count = 0; count < fields.length; count++ ){
            if(((count==1||count==2)&&size!=4)){
                fields[ count ] = new JTextField(5);
            }
            else fields[ count ] = new JTextField(10);
        }

        for ( int count = 0; count <= labels.length; count++ )
            innerPanel[count] = new JPanel();
        this.setLayout( new GridLayout(size+1,1) );

        for ( int count = 0; count < size; count++ ) {
            innerPanel[count].add( labels[ count ] );
            innerPanel[count].add( fields[ count ] );
            if(labels[count].getText().equals("会员号")
                    ||labels[count].getText().equals("员工号")
                    ||labels[count].getText().equals("图书编号")
                    ||labels[count].getText().equals("初始密码"))
            {
                fields[count].setEditable(false);
            }
            if(labels[count].getText().equals("性      别")){
                final JComboBox<String> comboBox;
                comboBox=new JComboBox<String>();
                comboBox.setModel(new DefaultComboBoxModel<String>(new String[]{"男","女"}));
                fields[2].setText((String) comboBox.getSelectedItem());
                innerPanel[count].add(comboBox);
                comboBox.addActionListener(
                        new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                fields[2].setText((String) comboBox.getSelectedItem());//设置文本框的内容
                            }//设置事件响应器

                        });
                //continue;
            }

            if(labels[count].getText().equals("等      级")){
                final JComboBox<String> comboBox;
                comboBox=new JComboBox<String>();
                comboBox.setModel(new DefaultComboBoxModel<String>(new String[]{"青铜会员","白银会员","黄金会员","钻石会员"}));
                fields[1].setText((String) comboBox.getSelectedItem());
                innerPanel[count].add(comboBox);
                comboBox.addActionListener(
                        new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                fields[1].setText((String) comboBox.getSelectedItem());//设置文本框的内容
                            }//设置事件响应器

                        });
                //continue;
            }
            if(labels[count].getText().equals("分     类")){
                final JComboBox<String> comboBox;
                comboBox=new JComboBox<String>();
                comboBox.setModel(new DefaultComboBoxModel<String>(new String[]{"教育","文艺","文学","童书","人文社科","经济","科学技术"}));
                fields[2].setText((String) comboBox.getSelectedItem());
                innerPanel[count].add(comboBox);
                comboBox.addActionListener(
                        new ActionListener() {

                            public void actionPerformed(ActionEvent e) {
                                // TODO Auto-generated method stub
                                fields[2].setText((String) comboBox.getSelectedItem());//设置文本框的内容
                            }//设置事件响应器

                        });
                //continue;
            }

        }

        doTask1 = new JButton();
        doTask2 = new JButton();
        innerPanel[size].add( doTask1 );
        innerPanel[size].add( doTask2 );
        for ( int count = 0; count <= fields.length; count++ )
            this.add(innerPanel[count]);
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        validate(); // validate layout
    }
    public void hideDoTask1Button()
    {
        doTask1.setVisible(false);
    }
    public void hideDoTask2Button()
    {
        doTask2.setVisible(false);
    }

    public JButton getDoTask1Button()
    {
        return doTask1;
    }

    // return reference to generic task button doTask2
    public JButton getDoTask2Button()
    {
        return doTask2;
    }
    public JTextField[] getFields()
    {
        return fields;
    }
    public void clearFields()
    {
        for ( int count = 0; count < size; count++ )
            fields[ count ].setText( "" );
    }
    public void setFieldValues( String strings[] )
            throws IllegalArgumentException
    {
        if ( strings.length != size )
            throw new IllegalArgumentException( "There must be " +
                    size + " Strings in the array" );

        for ( int count = 0; count < size; count++ )
            fields[ count ].setText( strings[ count ] );
    }
    public String[] getFieldValues()
    {
        String values[] = new String[ size ];

        for ( int count = 0; count < size; count++ )
            values[ count ] = fields[ count ].getText();

        return values;
    }
}



